/*
 * @Author: i@douxuefeng.cn
 * @Date: 2021-04-16 15:25:14
 * @LastEditTime: 2021-10-12 12:03:38
 * @LastEditors: i@douxuefeng.cn
 * @Description:
 * @FilePath: \go-utils\jsonData.go
 */

package go_utils

type JsonData struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

func Json(code int, data interface{}, msg string) *JsonData {
	return &JsonData{
		Code: code,
		Data: data,
		Msg:  msg,
	}
}

func JsonNoData(code int, msg string) *JsonData {
	return &JsonData{
		Code: code,
		Msg:  msg,
	}
}

func JsonErr(code int, err error) *JsonData {
	return &JsonData{
		Code: code,
		Msg:  err.Error(),
	}
}

func Response(code int, err error, msg string, data interface{}) *JsonData {
	if err != nil {
		return JsonErr(code, err)
	}
	return Json(200, data, msg)
}
